#include "StdAfx.h"
#include "Helicopter.h"
#include <sstream>

CHelicopter::CHelicopter(void)
{
	 p_x=  p_y=  p_z = 3;
	_moveX=_moveY=_moveZ=0;
	_rotation=0;
	MaxHostages=6;
	_savedHostages=0;
}

CHelicopter::~CHelicopter(void)
{
}

void CHelicopter::Render(void)
{
	glTranslated(_moveX, _moveY, _moveZ);

      glTranslatef(2.0f, 0, 2.0f);
	glRotated(_rotation,0,1,0);
	//glTranslated(-1., 0., -1.);
	glScalef(1.0f / 3.0f, 1.0f / 4.0f, 1.0f / 4.0f);
	DrawHeli(1, 1, 1);

}

void CHelicopter::Update(double dt)
{
	double rad_angle = (3.141592654f * getRotation()/180);

	if (GetKeyState(VK_LEFT) < 0)        // left arrow
		setRotation(5);
	if (GetKeyState(VK_RIGHT) < 0)        // right
		setRotation(-5);
	if (GetKeyState(VK_UP) < 0)        // up, go forward
		setPosition(-(.2*sin(rad_angle)),0,-(.2*cos(rad_angle)));
	if (GetKeyState(VK_DOWN) < 0)        // down, go backwards
		setPosition((.2*sin(rad_angle)),0,(.2*cos(rad_angle)));

	if (GetKeyState(0x057) < 0)        // w, go up
		setPosition(0,.2,0.);
	if (GetKeyState(0x053) < 0)        // s, go down
		setPosition(0,-.2,0.);

	if (GetKeyState(0x041) < 0)        // a, strate left
		setPosition(-(.2*cos(rad_angle)),0,(.2*sin(rad_angle)));
	if (GetKeyState(0x044) < 0)        // d, strafe right
		setPosition((.2*cos(rad_angle)),0,-(.2*sin(rad_angle)));
}

void CHelicopter::DrawHeli(GLdouble p_x, GLdouble p_y, GLdouble p_z)
{
//*********Based on code online for planes*************
  glClear(GL_DEPTH_BUFFER_BIT);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_FLAT);

      glPushMatrix();
      glRotatef(290.0, 1.0, 0.0, 0.0);
      glScalef(1.0 / 2.0, 1.0 / 3.0, 1.0 / 3.0);
      glBegin(GL_TRIANGLE_STRIP);
      /* left wing */
      glVertex3f(-7.0, 0.0, 2.0);
      glVertex3f(-1.0, 0.0, 3.0);
      glColor3f(0.0 , 1.0 , 0.0);
      glVertex3f(-1.0, 7.0, 3.0);
      /* left side */
      glColor3f(0.0, .90, 0.0);
      glVertex3f(0.0, 0.0, 0.0);
	  glColor3f(0.0, .90, 0.0);
      glVertex3f(0.0, 8.0, 0.0);
      /* right side */
      glVertex3f(1.0, 0.0, 3.0);
      glVertex3f(1.0, 7.0, 3.0);
      /* final tip of right wing */
      glColor3f(0.0,1.0, 0.0);
      glVertex3f(7.0, 0.0, 2.0);
      glEnd();
      glPopMatrix();
//*********END*************




}


void CHelicopter::setPosition(GLdouble x, GLdouble y, GLdouble z)
{
	_moveX+=x;
	if (_moveY>=.5 ||y>0){_moveY+=y;}

	_moveZ+=z;
}

void CHelicopter::addSavedHostages(int n)
{
	_savedHostages += n;

	/*
	int cantsave = 0;
	_savedHostages += n;
	if (_savedHostages >= MaxHostages)
	{
		cantsave = _savedHostages - MaxHostages;
		_savedHostages = MaxHostages;
	}
	return cantsave;
	*/
}